package com.amazon.shop.exception;

import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.util.ResultUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.validation.BindException;

/**
 * 创建时间：10:50 2021/1/13
 *
 * @author lzj
 * @since JDK 1.8.0_171
 * 类说明：
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 */
@Log4j2
@ControllerAdvice
@ResponseBody
public class BusinessExceptionHandler {


    /**
     * 全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    public Ret bindException(BindException e) {
        log.error("参数异常日志：{}",e.getMessage());
        return ResultUtil.error("-1","-1",e.getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 参数异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Ret hadlerException(Exception e) {
        log.error("全局异常日志：{}",e.getMessage());
        return ResultUtil.error();
    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public Ret handlerEastHorhException(BusinessException e) {
        return ResultUtil.error(e.getCode(),e.getServecode(), e.getMessage());
    }
}
